package org.light.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.generator.MybatisSqlReflector;
import org.light.generator.NamedStatementGenerator;
import org.light.generator.NamedStatementListGenerator;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class FindFullInfoById extends Verb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception{
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName(
					"find" + this.domain.getStandardName() + "FullInfoBy" + this.domain.getDomainId().getCapFirstFieldName());
			method.setReturnType(new Type(this.domain.getCapFirstDomainNameWithSuffix(),this.domain.getPackageToken()));
			method.addAdditionalImport("java.sql.Connection");
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport("java.util.ArrayList");
			method.addAdditionalImport("java.sql.PreparedStatement");
			method.addAdditionalImport("java.sql.ResultSet");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+ this.domain.getDomainSuffix() +"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoSuffix()+"."+this.domain.getStandardName()+"Dao");
			method.addSignature(new Signature(1,"connection",new Type("Connection","java.sql"),"java.sql"));
			method.addSignature(new Signature(2,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getRawType()));
			method.setThrowException(true);
			
			List<Writeable> list = new ArrayList<Writeable>();
			list.add(NamedStatementGenerator.getTryHead(1000L,2));
			list.add(NamedStatementListGenerator.generateFindByIdStatementList(2000L,3,this.domain));
			list.add(NamedStatementListGenerator.generateCatchExceptionPrintStackReturnNullFooter(3000L,2));
			method.setMethodStatementList(WriteableUtil.merge(list));
			method.setMethodStatementList(WriteableUtil.merge(list));
			return method;
		}
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception{
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName(
					"find" + this.domain.getStandardName() + "FullInfoBy" + this.domain.getDomainId().getCapFirstFieldName());
			method.setReturnType(
					new Type(this.domain.getCapFirstDomainNameWithSuffix(), this.domain.getPackageToken()));
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
					+ this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1,"connection",new Type("Connection","java.sql"),"java.sql"));
			method.addSignature(new Signature(2, this.domain.getDomainId().getFieldName(),
					this.domain.getDomainId().getClassType()));
			method.setThrowException(true);
			return method;
		}
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception{
		return null;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception{
		return null;
	}

	public FindFullInfoById(){
		super();
		this.setLabel("查找全部信息");
	}
	
	public FindFullInfoById(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("Clone");
		this.setVerbName("Find"+StringUtil.capFirst(this.domain.getStandardName())+"FullInfoBy"+this.domain.getDomainId().getCapFirstFieldName());
		this.setLabel("查找全部信息");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("FindFullInfoById");
	}

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;		
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName(
					"find" + this.domain.getStandardName() + "FullInfoBy" + this.domain.getDomainId().getCapFirstFieldName());
			method.setReturnType(
					new Type(this.domain.getCapFirstDomainNameWithSuffix(), this.domain.getPackageToken()));
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
					+ this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1, this.domain.getDomainId().getFieldName(),
					this.domain.getDomainId().getClassType()));
			method.setThrowException(true);
			
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(2000L,2,"for ("+this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+" : db) {"));
			sList.add(new Statement(3000L,3,"if ("+this.domain.getLowerFirstDomainName()+".get"+this.domain.getDomainId().getCapFirstFieldName()+"().equals("+this.domain.getDomainId().getLowerFirstFieldName()+")) return "+this.domain.getLowerFirstDomainName()+";"));
			sList.add(new Statement(4000L,2,"}"));
			sList.add(new Statement(5000L,2,"return null;"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyServiceImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyDaoMethodDefinition() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName(
					"find" + this.domain.getStandardName() + "FullInfoBy" + this.domain.getDomainId().getCapFirstFieldName());
			method.setReturnType(
					new Type(this.domain.getCapFirstDomainNameWithSuffix(), this.domain.getPackageToken()));
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
					+ this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1, this.domain.getDomainId().getFieldName(),
					this.domain.getDomainId().getClassType()));
			method.setThrowException(true);
			return method;
		}
	}	
}
